package win.larryzeal.redisdemo.service;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Service;
import win.larryzeal.redisdemo.pojo.City;
import win.larryzeal.redisdemo.pojo.Country;


/*
 * 监听器，用于项目启动的时候初始化信息
 */
@Service
public class StartAddCacheListener implements ApplicationListener<ContextRefreshedEvent> {
	//日志
	private final Logger log = LoggerFactory.getLogger(StartAddCacheListener.class);

	@Autowired
	private RedisCacheUtil<Object> redisCache;

	@Autowired
	private SomeService someService;

	@Override
	public void onApplicationEvent(ContextRefreshedEvent event){
		//spring 启动的时候缓存城市和国家等信息
		if(event.getApplicationContext().getDisplayName().equals("Root WebApplicationContext")){ //StaticWebApplicationContext

			List<City> cityList = someService.selectAllCity();
			List<Country> countryList = someService.selectAllCountry();

			Map<Integer, City> cityMap = new HashMap<Integer, City>();
			Map<Integer, Country> countryMap = new HashMap<Integer, Country>();

			int cityListSize = cityList.size();
			int countryListSize = countryList.size();

			for(int i = 0; i < cityListSize; i++){
				cityMap.put(cityList.get(i).getCity_id(), cityList.get(i));
			}

			for(int i = 0; i < countryListSize; i++){
				countryMap.put(countryList.get(i).getCountry_id(), countryList.get(i));
			}

			redisCache.setCacheIntegerMap("cityMap", cityMap);
			redisCache.setCacheIntegerMap("countryMap", countryMap);
		}
	}

}